package com.sgb.member.view_model;

import com.sgb.member.MemberConfig;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.entity.MemberPersonBean;
import com.sgb.member.ui.fragment.PersonAuditFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * Created by：冯齐 on 2020/12/31 16:21
 * Description：
 */
public class PersonAuditModel extends BaseViewModel {

    PersonAuditFragment fragment;
    MemberRepertory repertory;
    private int current;

    public PersonAuditModel(PersonAuditFragment fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    /**
     * 审核列表
     *
     * @param type      加盟类型:1申请加盟 2邀请加盟
     * @param checkType 审核类型:1 个人 2 企业
     * @param status    审核状态:1审批中 2审批 3审批拒绝
     */
    public void getCheckMember(boolean isLoadMore, String type, String checkType, String status) {
        int currentPage = isLoadMore ? current + 1 : 1;
        Map<String, Object> map = new HashMap<>();
        map.put("joinType", type);
        map.put("joinWay", type);
        map.put("checkType", checkType);
        map.put("status", status);
        map.put("limit", 20);
        map.put("page", currentPage);
        DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
        repertory.getApi().getPersonCheckMember(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<MemberPersonBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<MemberPersonBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<MemberPersonBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                if(!isLoadMore){
                    fragment.showEmpty();
                }
            }
        });
    }

    //获取h5链接
    /**
     * ### 申请审核--企业主体
     * 路由地址:``/companyExamine``
     * 需要传参:
     * 1.no,为列表数据项no
     * 2.companyNo,为列表数据项companyNo
     * 3.isExamine,从会员审核列表进入传1
     * 4.joinWay      1申请   2邀请
     *
     * ### 申请审核--个人主体
     * 路由地址:``/personalExamine``
     * 需要传参:
     * 1.no,为列表数据项no
     * 2.userNo,为列表数据项userNo
     * 3.isExamine,从会员审核列表进入传1
     * 4.joinWay      1申请   2邀请
     */
    public void getUrlSetting(String param, String endUrl) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().getUrlSetting(param).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + endUrl;
                MemberConfig.getInstance().startWebViewX5(fragment.getActivity(), url, 101);
            }
        });
    }

    public void getInviteOrApplyStatus() {
        // 邀请状态：待审核、审核通过、审核未通过
        // 申请状态：待审核、审核通过、审核未通过
        DialogHelper.showProgressDialog(fragment.getActivity(), "数据加载中...");
        repertory.getApi().getCheckStatus().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setInviteOrApplyStatus(data.getData());
                }
            }
        });
    }

    public void getMemberMainType() {
        // 会员主体
        List<CommonPopEntity> listMain = new ArrayList<>();
        listMain.add(new CommonPopEntity("2", "企业会员"));
        listMain.add(new CommonPopEntity("1", "个人会员"));
        fragment.setMemberMainType(listMain);
    }

    public void getAuditType() {
        // 会员主体  // 1 会员申请  2企业邀请
        List<CommonPopEntity> listMain = new ArrayList<>();
        listMain.add(new CommonPopEntity("", "全部"));
        listMain.add(new CommonPopEntity("2", "会员邀请"));
        listMain.add(new CommonPopEntity("1", "会员申请"));
        fragment.setAuditType(listMain);
    }
}
